package com.sdp.th.cwt;

import java.math.BigDecimal;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.th.cache.ThCleanCache;
import com.sdp.th.cache.caches.EqStateCache;
import com.sdp.th.db.th_eq_alarm_smoke_log.service.IThEqAlarmSmokeLogService;
import com.sdp.th.db.th_eq_message.service.IThEqMessageService;
import com.sdp.th.db.th_eq_power.service.IThEqPowerService;
import com.sdp.th.db.th_eq_state.service.IThEqStateService;
import com.sdp.th.entity.ThEqAlarmSmokeLog;
import com.sdp.th.entity.ThEqMessage;
import com.sdp.th.entity.ThEqPower;
import com.sdp.th.entity.ThEqState;
import com.sdp.th.utils.ThConstants;
import com.sdp.th.utils.Utils;

import akka.actor.UntypedActor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CWTActor extends UntypedActor{

	@Override
	public void onReceive(Object message) throws Exception {
		try {
			JSONObject jsonData = (JSONObject) message;
			
			log.info("解析后的json数据:{}",jsonData);
			
			ThEqState eqState = new ThEqState();
			eqState.setDeviceNo(jsonData.getString("deviceNo"));
			eqState.setReportTime(Utils.timestampToLocalDateTime(jsonData.getLongValue("timestamp")));
			eqState.setLineState(ThConstants.是.getCode());
			eqState.setAlarmState(ThConstants.否.getCode());
			
			//如果原始报文存在
			if(!StringUtils.isEmpty(jsonData.getString("originalMessage"))) {
				eqState.setOriginalMessage(jsonData.getString("originalMessage"));
				
				JSONObject stateObj = ISNBParser.smokeState(jsonData.getString("originalMessage"));
				
				if(!StringUtils.isEmpty(stateObj.getString("devicePower"))) {
					eqState.setDevicePower(new BigDecimal(stateObj.getString("devicePower")));
				}
				
				if(stateObj.getBooleanValue("smokeAlarm")) {
					eqState.setAlarmState(ThConstants.是.getCode());
					
					//增加烟感报警信息
					log.info("*********烟感报警,添加烟感报警信息**********");
					ThEqAlarmSmokeLog smokeLog = new ThEqAlarmSmokeLog();
					smokeLog.setDeviceNo(eqState.getDeviceNo());
					smokeLog.setCreateTime(eqState.getReportTime());
					smokeLog.setOriginalMessage(eqState.getOriginalMessage());
					smokeLog.setAlarmAction(ThConstants.报警处理_未处理.getCode());
					ApplicationContextUtils.getBean(IThEqAlarmSmokeLogService.class).verifySmokeAlarm(smokeLog);
					log.info("**************《烟感报警》业务完成");
					
				}else {
					eqState.setAlarmState(ThConstants.否.getCode());
				}
				
				//低电量报警
//				if(stateObj.getBooleanValue("powerAlarm")) {
//					log.info("*********低电量报警，添加低电量信息**********");
//					if(!StringUtils.isEmpty(stateObj.getString("devicePower"))) {
//						eqState.setDevicePower(new BigDecimal(stateObj.getString("devicePower")));
//					}
//					ThEqPower eqPower = new ThEqPower();
//					eqPower.setDeviceNo(eqState.getDeviceNo());
//					eqPower.setCreateTime(eqState.getReportTime());
//					eqPower.setDeviceType(ThConstants.设备类型_烟感探测器.getCode());
//					ApplicationContextUtils.getBean(IThEqPowerService.class).insertSmokePower(eqPower);
//					log.info("**************《烟感低电量报警》业务完成");
//				}
				
			}


			//更新实时信息
			ThEqState dbThEqState = ApplicationContextUtils.getBean(IThEqStateService.class).updateState(eqState);
			log.info("**************更新烟感《实时信息》业务完成");
			
			//更新缓存
			if(dbThEqState==null) {
				//删除缓存
				ApplicationContextUtils.getBean(ThCleanCache.class).cleanEqStateCache(eqState.getDeviceNo());
			}else {
				ApplicationContextUtils.getBean(EqStateCache.class).updateStateCache(dbThEqState);
			}
			log.info("**************更新烟感《实时缓存》业务完成");
			
			//添加原始报文数据
			ThEqMessage thEqMessage = new ThEqMessage();
			thEqMessage.setCreateTime(eqState.getReportTime());
			thEqMessage.setOriginalMessage(jsonData.getString("data"));
			thEqMessage.setDeviceNo(eqState.getDeviceNo());
			ApplicationContextUtils.getBean(IThEqMessageService.class).save(thEqMessage);
			log.info("**************数据库添加《原始报文》业务完成");
		} catch (Exception e) {
			log.error("CWTActorActor执行出现异常：{}",Utils.stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));
		}
		
	}

}
